package com.project.webapp.action.front;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.project.common.Constants;
import com.project.service.front.ILoginService;
import com.project.webapp.action.BaseAction;
import com.project.webapp.form.front.LoginForm;

public class LoginAction extends BaseAction {

	/**
	 * <pre>
	 * 
	 * </pre>
	 */
	private static final long serialVersionUID = 1L;
	private LoginForm loginForm;
	// 登录结果
	private String result = "";
	// 下次是否自动登录
	private String autoLogin = "";
	// 登入窗口flag，1-首画面登入，2-子画面登入
	private String loginFlag = "";
	// 登入失败计数
	private String failCount = "1";
	// 显示验证码
	private String isShowImage = "";

	public LoginAction() {
		if (loginForm == null) {
			loginForm = new LoginForm();
		}
		if ("".equals(failCount)) {
			failCount = "0";
		}
	}

	ILoginService loginService;

	public String login() {
		try {
			boolean flag = loginService.validLoginCustomer(loginForm);
			if (flag) {
				// 登录成功
				result = "loginSuc";
				// 设置下次是否自动登录cookie
				if ("true".equals(autoLogin)) {
					failCount = "0";
					HttpServletResponse res = ServletActionContext
							.getResponse();
					// 将登入帐号保存至cookie
					Cookie idCookie = new Cookie(
							Constants.PUDU_COOKIE_LOGIN_ID,
							loginForm.getCust_id());
					idCookie.setPath("/");
					idCookie.setMaxAge(14 * 24 * 3600);// 有效时间，单位是秒
					res.addCookie(idCookie);
					// 将登入密码保存至cookie
					Cookie pwdCookie = new Cookie(
							Constants.PUDU_COOKIE_LOGIN_PWD,
							loginForm.getCust_pwd());
					pwdCookie.setPath("/");
					pwdCookie.setMaxAge(14 * 24 * 3600);// 有效时间，单位是秒
					res.addCookie(pwdCookie);
				}
			} else {
				// 登录失败
				if ("1".equals(loginFlag)) {
					result = "loginFail";
				} else if ("2".equals(loginFlag)) {
					result = "subLoginFail";
					if ("".equals(failCount)) {
						failCount = "0";
					}
					failCount = (Integer.parseInt(failCount) + 1) + "";
					if (Integer.parseInt(failCount) > 2) {
						isShowImage = "yes";
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return result;
	}

	public LoginForm getLoginForm() {
		return loginForm;
	}

	public void setLoginForm(LoginForm loginForm) {
		this.loginForm = loginForm;
	}

	public ILoginService getLoginService() {
		return loginService;
	}

	public void setLoginService(ILoginService loginService) {
		this.loginService = loginService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getAutoLogin() {
		return autoLogin;
	}

	public void setAutoLogin(String autoLogin) {
		this.autoLogin = autoLogin;
	}

	public String getLoginFlag() {
		return loginFlag;
	}

	public void setLoginFlag(String loginFlag) {
		this.loginFlag = loginFlag;
	}

	public String getFailCount() {
		return failCount;
	}

	public void setFailCount(String failCount) {
		this.failCount = failCount;
	}

	public String getIsShowImage() {
		return isShowImage;
	}

	public void setIsShowImage(String isShowImage) {
		this.isShowImage = isShowImage;
	}

}
